package se.webbzon.boltzmann.editor.list;

import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.vecmath.Point3d;

import org.jdesktop.swingx.graphics.BlendComposite;

import se.webbzon.boltzmann.object.geometry.Geometry;
import se.webbzon.oschi01.worldeditor.DeflatableWorldObject;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;

public class InstanceListCell extends EditorListCell implements ListCellRenderer<WorldObject> {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = 389918639622102441L;
	
	// The position used for printing
	private final Point3d position;
	
	// The world object associated with this list cell
	private WorldObject instance;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public InstanceListCell() {
		super(new Dimension(128,20));
		position = new Point3d();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setFont(getFont());
		String text;
		if (instance == null)
			text = "No Instance (null)";
		else if (instance instanceof DeflatableWorldObject)
			text = ((DeflatableWorldObject) instance).getName();
		else 
			text = instance.getClass().getCanonicalName();
		int x = 2;
		
		if (instance instanceof WorldStaticObject) {
			WorldStaticObject wso = ((WorldStaticObject) instance);
			wso.getPosition(position);
			text += " " + position;
			
			if (wso instanceof Geometry) {
				SimpleAppearance3D appearance = 
						((Geometry) wso).getGeometryAppearance().getAppearance();
				x += 20;
				
				if (appearance.getTextureSet() == null) {
					g.setColor(appearance.getColor());
					g.fillRect(2, 2, 16, 16);
				} else {
					Graphics2D g2d = (Graphics2D) g;
					g2d.setColor(appearance.getColor());
					g2d.fillRect(2, 2, 16, 16);
					Composite c = g2d.getComposite();
					g2d.setComposite(BlendComposite.Average);
					g2d.drawImage(	appearance.getTextureSet().get(0).getImage(), 
									2, 2, 16, 16, this);
					g2d.setComposite(c);

				}
				if (wso.isVisible())
					g.setColor(Color.black);
				else
					g.setColor(Color.green);
				g.drawRect(2, 2, 16, 16);
				if (wso.isSolid()) {
					g.setColor(Color.red);
					g.drawRect(1, 1, 18, 18);
				}
			}
		}
		g.drawString(text, x, 16);
	}

	@Override public Component getListCellRendererComponent(
			JList<? extends WorldObject> list, WorldObject instance, int index,
			boolean isSelected, boolean cellHasFocus) {
		setSelected(isSelected);
		this.instance = instance;
		return this;
	}

}
